﻿using UnityEngine;
using System.Collections.Generic;

public class Damage {

    private Character Target { get; set; }

    public Damage(Character target) {
        Target = target;
    }

    public float AddRate { get; set; }

    //火水雷风，混乱
    public enum DamageType { Fire,Water,Thunder,Wind,Chaos,Length }
    public Dictionary<DamageType, float> DamageValue = new Dictionary<DamageType, float>();
    public Dictionary<DamageType, float> SumDamageValue = new Dictionary<DamageType, float>();

    public void AddDamage(int typeInt, float value)
    {
        if ((int)DamageType.Length <= typeInt) { 
            Debug.Log("不存在的伤害类型！");
            return;
        }
            
        DamageType type = (DamageType)typeInt;
        AddDamage(type, value);
    }

    public void AddDamage(DamageType type, float value) {
        if (DamageValue.ContainsKey(type))
        {
            DamageValue[type] += value;
        }
        else {
            DamageValue.Add(type, value);
        }
    }

    //合并伤害
    private void SumDamage() {
        foreach (var data in DamageValue) {
            if (SumDamageValue.ContainsKey(data.Key))
            {
                SumDamageValue[data.Key] += data.Value;
            }
            else {
                SumDamageValue.Add(data.Key, data.Value);
            }
        }
        Dictionary<DamageType, float> tmpDic = new Dictionary<DamageType, float>();
        //将加成系数加上去
        foreach (var data in SumDamageValue)
        {
            tmpDic.Add(data.Key,data.Value * (1.0f + AddRate));
        }
        SumDamageValue = tmpDic;
    }

    public void Active()
    {
        //先计算伤害总和
        SumDamage();
        //分别计算防御，并造成伤害
        foreach (var data in SumDamageValue)
        {
            float def = Target.GetDef(data.Key);
            float lastDamage = data.Value * (100.0f - def) / 100.0f;
            lastDamage = lastDamage < 1 ? 1 : lastDamage;
            Target.AddHp(-lastDamage);
        }
    }
}
